package cn.signalpha.modules.social.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.social.dto.CommentDTO;
import cn.signalpha.modules.social.entity.Comment;
import cn.signalpha.modules.social.service.CommentService;
import cn.signalpha.modules.social.vo.CommentVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SOCIAL_NAME + "/admin/comment")
@Api(value = "评论", tags = "评论")
public class CommentController extends BaseController {

	private final CommentService commentService;

    @GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="targetId", dataType = "string", value = "主题id"),
	})
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<CommentVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        return R.data(commentService.selectCommentPage(Condition.getPage(query), params));
    }

	@GetMapping("/reply/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="parentId", dataType = "string", value = "评论id"),
	})
	@ApiOperation(value = "回复列表", notes = "")
	public R<IPage<CommentVO>> replyPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		return R.data(commentService.selectCommentReplyPage(Condition.getPage(query), params));
	}

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<Comment> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(commentService.getById(id));
	}

	@PostMapping("/show")
	@ApiOperation(value = "显示隐藏", notes = "")
	public R show(@Valid @RequestBody CommentDTO.IsShowDTO isShowDTO) {
		Comment comment = Comment.builder().isShow(isShowDTO.getIsShow()).build();
		return R.status(commentService.update(comment, Wrappers.<Comment>lambdaUpdate().in(Comment::getId, Func.toStrList(isShowDTO.getId()))));
	}

	@PostMapping("/top")
	@ApiOperation(value = "置顶", notes = "")
	public R top(@Valid @RequestBody CommentDTO.IsTopDTO isTopDTO) {
		Comment comment = Comment.builder().isTop(isTopDTO.getIsTop()).build();
		return R.status(commentService.update(comment, Wrappers.<Comment>lambdaUpdate().in(Comment::getId, Func.toStrList(isTopDTO.getId()))));
	}

	@PostMapping("/audit")
	@ApiOperation(value = "审核", notes = "传入id和status")
	public R audit(@Valid @RequestBody CommentDTO.AuditDTO auditDTO) {
		Comment comment = Comment.builder().status(auditDTO.getStatus()).extend(auditDTO.getRemark()).build();
		return R.status(commentService.update(comment, Wrappers.<Comment>lambdaUpdate().in(Comment::getId, Func.toStrList(auditDTO.getId()))));
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(commentService.removeByIds(Func.toLongList(ids)));
	}

}
